<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"
    />
    <title>设计原则与编程技巧</title>
</head>

<body>
    <strong>开放封闭原则</strong>
    <p> Attack即为大型类</p>


    <script type="text/javascript">
        var Attack = function (spirit) {
            this.spirit = spirit // spirit ：角色
        }
        Attack.prototype.start = function (type) {
            if (this.list.hasOwnProperty(type)) {
                return this.list[type].call(this)
            } else {
                console.log(this.spirit.name + "还没学会 " + type);
            }
        }
        Attack.prototype.list = {
            waveBoxing: function () {
                console.log(this.spirit.name + "使用波动拳");
            },
            whirlKick: function () {
                console.log(this.spirit.name + '使用旋风腿')
            }
        }

        var Spirit = function (name) {  // 创建角色
            this.name = name;
            this.attackObj = new Attack(this)  // 继承攻击
        }
        Spirit.prototype.attack = function (type) {
            this.attackObj.start(type)
        }

        var spirit = new Spirit('RYU')
        spirit.attack('waveBoxing')
        spirit.attack('whirlKick')
        spirit.attack('哈哈大笑')



    </script>

</body>

</html>